package com.zhaopin.leetcode;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 使用linkedHashMap实现lru算法
 */
public class LruTests2 extends LinkedHashMap<String,Object>{

    private int initialCapacity;

    public LruTests2(int initialCapacity) {
        super(initialCapacity, 0.75F, true);
        this.initialCapacity = initialCapacity;
    }

    public void setLru(String str,Object o){
        super.put(str,o);
    }

    public Object getLru(String str){
        return super.getOrDefault(str,"");
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<String, Object> eldest) {
        return size() > initialCapacity;
    }


    public static void main(String[] args) {

        //进行测试
        LruTests2 lruTests = new LruTests2(3);
        lruTests.setLru("23","23");
        lruTests.setLru("44","44");
        lruTests.setLru("11","11");
        lruTests.setLru("22","22");
        System.out.println(lruTests.toString());
        System.out.println(lruTests.getLru("11"));
        System.out.println(lruTests.toString());

    }
}
